package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserAnnotation;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.redis.Service.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService redisService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("query")
    public Object query(TravelQuery qo) {
        //根据id查询攻略明细
        return JsonResult.success(travelService.query(qo));
    }

    @RequireLogin
    @GetMapping("input")
    public Object input(String id) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.hasLength(id)) {
            //根据id查询所对应的游记
            Travel travel = travelService.selectOne(id);
            map.put("tv", travel);
        }
        //查询所有目的地
        List<Destination> dests = destinationService.list();
        map.put("dests", dests);
        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        UserInfo user = redisService.getUserByToken(token);
        if (user != null) {
            travel.setAuthor(user);
        }
        travelService.saveOrUpdate(travel);
        //根据id查询攻略明细
        return JsonResult.success(travel.getId());
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }


    @GetMapping("detail")
    public Object detail(String id) {
        //根据游记id查询对应的游记信息
        Travel travel = travelService.queryTrave(id);
        return JsonResult.success(travel);
    }


    @GetMapping("comments")
    public Object comments(String travelId) {
        //根据游记id查询对应的游记评论信息
        List<TravelComment> travels = travelCommentService.selectByTId(travelId);
        //不需要分页,所以返回即可
        return JsonResult.success(travels);
    }

    @RequireLogin
    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment comment, @UserAnnotation UserInfo userInfo) {
        //将当前用户信息设置进评论对象中
        BeanUtils.copyProperties(userInfo, comment);
        //用户id要重新设置
        comment.setUserId(userInfo.getId());
        travelCommentService.commentAdd(comment);
        return JsonResult.success();
    }


}